package View;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import Model.Helicopter;
import Model.Radar;
import Model.Wereld;

@SuppressWarnings("serial")
public class LuchtruimView extends JPanel implements Observer {
        private double kmPerPixel = 1;
        private double viewSize = 800;
        private Wereld wereld;
        private BufferedImage bImage;
        private Muis muis;

        public LuchtruimView(Wereld wereld) {
    			muis = new Muis();
                wereld.addObserver(this);
                this.wereld = wereld;
                setBackground(Color.BLACK);
                File file = new File("Data/background.png");
                try {
                        bImage = ImageIO.read(file);
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }

        public void drawHelicopter(Graphics g) {
                g.setColor(Color.GREEN);
                Helicopter heli = wereld.getHelicopter();

                double a = Math.sqrt((heli.getLookX() * heli.getLookX())
                                + heli.getLookY() * heli.getLookY());

                double nx = heli.getLookX() / a * 20;
                double ny = heli.getLookY() / a * 20;
                
                double wx = Math.sin(heli.getWiekHoek()/180 * 3.14159)*15;
                double wy = Math.cos(heli.getWiekHoek()/180 * 3.14159)*15;
                double wx2 = Math.sin((heli.getWiekHoek()+90)/180 * 3.14159)*15;
                double wy2 = Math.cos((heli.getWiekHoek()+90)/180 * 3.14159)*15;
                
                if(heli.isZichtbaar())
                {
                        //staart
                g.fillOval((int) (heli.getPosX() / kmPerPixel - 5), (int) (heli
                                .getPosY()
                                / kmPerPixel - 5), (int) (10 / kmPerPixel),
                                (int) (10 / kmPerPixel));
                //cabine
                g.drawLine((int) (heli.getPosX() / kmPerPixel),
                                   (int) (heli.getPosY() / kmPerPixel),
                                   (int) ((heli.getPosX() - nx) / kmPerPixel), 
                                   (int) ((heli.getPosY() - ny) / kmPerPixel));
                //wieken
                g.drawLine((int) ((heli.getPosX() + wx) / kmPerPixel),
                                   (int) ((heli.getPosY() + wy) / kmPerPixel),
                                   (int) ((heli.getPosX() - wx) / kmPerPixel), 
                                   (int) ((heli.getPosY() - wy) / kmPerPixel));
                g.drawLine((int) ((heli.getPosX() + wx2) / kmPerPixel),
                                   (int) ((heli.getPosY() + wy2) / kmPerPixel),
                                   (int) ((heli.getPosX() - wx2) / kmPerPixel), 
                                   (int) ((heli.getPosY() - wy2) / kmPerPixel));
                
                }
                if(heli.isManouvreerbaar())
                {
                        Graphics2D g2d = (Graphics2D)g;
                        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
                        g2d.fillOval((int) (heli.getPosX() / kmPerPixel - 5), (int) (heli
                                        .getPosY()
                                        / kmPerPixel - 5), (int) (10 / kmPerPixel),
                                        (int) (10 / kmPerPixel));
                        g2d.drawLine((int) (heli.getPosX() / kmPerPixel),
                                           (int) (heli.getPosY() / kmPerPixel),
                                           (int) ((heli.getPosX() - nx) / kmPerPixel), 
                                           (int) ((heli.getPosY() - ny) / kmPerPixel));
                        //wieken
                        g2d.drawLine((int) ((heli.getPosX() + wx) / kmPerPixel),
                                           (int) ((heli.getPosY() + wy) / kmPerPixel),
                                           (int) ((heli.getPosX() - wx) / kmPerPixel), 
                                           (int) ((heli.getPosY() - wy) / kmPerPixel));
                        g2d.drawLine((int) ((heli.getPosX() + wx2) / kmPerPixel),
                                           (int) ((heli.getPosY() + wy2) / kmPerPixel),
                                           (int) ((heli.getPosX() - wx2) / kmPerPixel), 
                                           (int) ((heli.getPosY() - wy2) / kmPerPixel));
                        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
                }
                if(!heli.checkBounds() && !heli.isManouvreerbaar())
                {
                        g.setColor(Color.RED);
                        g.fillRect(0, 0, (int) (800 / kmPerPixel), (int) (800 / kmPerPixel));
                        g.setColor(Color.BLACK);
                        Font font = new Font("Arial", Font.BOLD, (int) (100 / kmPerPixel));
                        g.setFont(font);
                        g.drawString("YOU LOSE!", (int) (100 / kmPerPixel), (int) (300 / kmPerPixel));
                }
        }

        public void drawRadars(Graphics g) {
                ArrayList<Radar> radars = wereld.getRadarlijst();
                for (Radar radar : radars) {
                        g.setColor(Color.GREEN.darker().darker().darker());
                        g.drawOval((int) (radar.getManouvreerbaar().getX() / kmPerPixel),
                                        (int) (radar.getManouvreerbaar().getY() / kmPerPixel),
                                        (int) (radar.getManouvreerbaar().getWidth() / kmPerPixel),
                                        (int) (radar.getManouvreerbaar().getHeight() / kmPerPixel));

                }
                for (Radar radar : radars) {
                        g.setColor(Color.GREEN);
                        g.drawOval((int) (radar.getZicht().getX() / kmPerPixel),
                                        (int) (radar.getZicht().getY() / kmPerPixel), (int) (radar
                                                        .getZicht().getWidth() / kmPerPixel), (int) (radar
                                                        .getZicht().getHeight() / kmPerPixel));
                }
                for (Radar radar : radars) {
                        g.setColor(Color.WHITE);
                        g.fillOval(
                                        (int) ((radar.getZicht().getCenterX() - 3) / kmPerPixel),
                                        (int) ((radar.getZicht().getCenterY() - 3) / kmPerPixel),
                                        (int) (6 / kmPerPixel), (int) (6 / kmPerPixel));
                }
        }

        public void paintComponent(Graphics g) {
                super.paintComponent(g);
                
                g.drawImage(bImage, 0, 0, (int) (800 / kmPerPixel),
                                (int) (800 / kmPerPixel), null);
                if(wereld.isEgg()){
                        Helicopter heli = wereld.getHelicopter();
                        g.setColor(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));
                        g.fillRect(0, 0, (int) (800 / kmPerPixel), (int) (800 / kmPerPixel));
                }
                
                drawRadars(g);
                drawHelicopter(g);
                g.setColor(Color.RED);
                Ellipse2D doel = wereld.getDoel();
                g.drawOval((int) (doel.getX() / kmPerPixel),
                                (int) (doel.getY() / kmPerPixel),
                                (int) (doel.getWidth() / kmPerPixel),
                                (int) (doel.getHeight() / kmPerPixel));
                if (wereld.isWin()) {
                        g.setColor(Color.GREEN);
                        g.fillRect(0, 0, (int) (800 / kmPerPixel), (int) (800 / kmPerPixel));
                        g.setColor(Color.BLACK);
                        Font font = new Font("Arial", Font.BOLD, (int) (100 / kmPerPixel));
                        g.setFont(font);
                        g.drawString("YOU WIN!", (int) (150 / kmPerPixel), (int) (300 / kmPerPixel));
                }
        }

        public void changeBounds() {
                kmPerPixel = viewSize / getBounds().width;
        }

        public void update(Observable o, Object obj) {
                changeBounds();
                repaint();
        }
        
        class Muis implements MouseListener
    	{
    		public void mouseClicked(MouseEvent e) {
    			requestFocusInWindow();
    		}

    		@Override
    		public void mouseEntered(MouseEvent e) {
    			// TODO Auto-generated method stub
    			
    		}

    		@Override
    		public void mouseExited(MouseEvent e) {
    			// TODO Auto-generated method stub
    			
    		}

    		@Override
    		public void mousePressed(MouseEvent e) {
    			// TODO Auto-generated method stub
    			
    		}

    		@Override
    		public void mouseReleased(MouseEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
    		
    	}
}